package com.cheer.wheelview;

import java.text.DecimalFormat;
import java.util.Calendar;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

public class DateSelectionDialog extends Dialog {

	/** 当前年 */
	private static int YEAR_START;
	/** 当前月 */
	private static int MOUTH_START;
	/** 当前天 */
	private static int DAY_START;
	/** 本月最大天数 */
	private static int DAY_END;
	/** 记录--当前年 */
	private static int YEAR;
	/** 记录--当前月 */
	private static int MOUTH;
	/** 记录--当前日 */
	private static int DAY;

	private WheelView wheelYear;

	private WheelView wheelMonth;

	private WheelView wheelDay;

	private Context mContext;

	private ChooseDateListener chooseDateListener;
	/** 判断是否是当前日期 */
	private boolean istrue;

	public DateSelectionDialog(Context context) {
		super(context, R.style.user_selected_dialog);
		mContext = context;
		View view = getPickerView();
		setContentView(view);
		setListener(view);

		WindowManager wm = ((Activity) context).getWindowManager();
		Display display = wm.getDefaultDisplay();
		WindowManager.LayoutParams lp = getWindow()
				.getAttributes();
		lp.width = display.getWidth() - DensityUtils.dip2px(context, 40);
		getWindow().setAttributes(lp);
	}

	/**
	 * @Description: TODO 弹出日期时间选择器
	 */
	public View getPickerView() {
		final Calendar calendar = Calendar.getInstance();
		YEAR_START = calendar.get(Calendar.YEAR);
		MOUTH_START = calendar.get(Calendar.MONTH);
		DAY_START = calendar.get(Calendar.DAY_OF_MONTH);
		DAY_END = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		YEAR = YEAR_START;
		MOUTH = MOUTH_START;
		DAY = DAY_START;
		View view = LayoutInflater.from(mContext).inflate(
				R.layout.dialog_date_pick_layout, null);

		wheelYear = (WheelView) view.findViewById(R.id.year);
		wheelMonth = (WheelView) view.findViewById(R.id.month);
		wheelDay = (WheelView) view.findViewById(R.id.day);

		NumericWheelAdapter yearAdapter = new NumericWheelAdapter(YEAR_START,
				YEAR_START + 100);
		NumericWheelAdapter monthAdapter = new NumericWheelAdapter(
				MOUTH_START + 1, 12);
		NumericWheelAdapter dayAdapter = new NumericWheelAdapter(DAY_START,
				DAY_END);

		// 年，转轮初始化
		wheelYear.setAdapter(yearAdapter);
		// 设置标签
		wheelYear.setLabel(mContext.getResources().getString(R.string.year));
		wheelYear.setLableOffest(-(int) mContext.getResources().getDimension(
				R.dimen.pxtodp40));
		// 循环
		wheelYear.setCyclic(false);
		// 当前项
		wheelYear.setCurrentItem(0);

		// 月，转轮初始化
		wheelMonth.setAdapter(monthAdapter);
		wheelMonth.setLabel(mContext.getResources().getString(R.string.month));
		wheelMonth.setLableOffest(-(int) mContext.getResources().getDimension(
				R.dimen.pxtodp30));
		wheelMonth.setCyclic(false);
		wheelMonth.setCurrentItem(MOUTH);

		// 日，转轮初始化
		wheelDay.setAdapter(dayAdapter);
		wheelDay.setLabel(mContext.getResources().getString(R.string.day));
		wheelDay.setLableOffest(-(int) mContext.getResources().getDimension(
				R.dimen.pxtodp20));
		wheelDay.setCyclic(false);
		wheelDay.setCurrentItem(DAY_START - 1);

		wheelYear.addChangingListener(new OnWheelChangedListener() {
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				YEAR = Integer
						.valueOf(wheelYear.getAdapter().getItem(newValue));
				checkMonth();
			}
		});

		wheelMonth.addChangingListener(new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				if (newValue == MOUTH_START) {
					MOUTH = newValue;
				} else {
					MOUTH = MOUTH_START + newValue;
				}
				checkDay();
			}
		});

		wheelDay.addChangingListener(new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				DAY = newValue;
			}
		});
		return view;
	}

	private void checkMonth() {
		if (YEAR == YEAR_START) {
			NumericWheelAdapter adapter = new NumericWheelAdapter(
					MOUTH_START + 1, 12);
			wheelMonth.setAdapter(adapter);
			if (MOUTH < MOUTH_START) {
				wheelMonth.setCurrentItem(MOUTH_START);
			} else {
				wheelMonth.setCurrentItem(MOUTH);
			}
		} else {
			NumericWheelAdapter adapter = new NumericWheelAdapter(1, 12);
			wheelMonth.setAdapter(adapter);
		}
		checkDay();
	}

	private void checkDay() {
		NumericWheelAdapter adapter = null;
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, YEAR);
		calendar.set(Calendar.MONTH, MOUTH);
		if (YEAR == YEAR_START && MOUTH == MOUTH_START) {
			// 如果年月相同
			adapter = new NumericWheelAdapter(DAY_START, DAY_END);
			wheelDay.setAdapter(adapter);
			// int nowDay = wheelDay.getAdapter().getItem(index);
			if (DAY < DAY_START) {
				// 选择比当前天小
				wheelDay.setCurrentItem(DAY_START);
			} else {
				// 选择比当前天大
				wheelDay.setCurrentItem(DAY - DAY_START);
			}
		} else {
			// 年月不一样
			int dayMax = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
			adapter = new NumericWheelAdapter(1, dayMax);
			wheelDay.setAdapter(adapter);
			if (DAY > dayMax) {
				// 当前
				wheelDay.setCurrentItem(dayMax - 1);
			} else {
				wheelDay.setCurrentItem(DAY);
			}
		}
	}

	public String getTimeString() {
		String year = wheelYear.getAdapter()
				.getItem(wheelYear.getCurrentItem());
		String month = wheelMonth.getAdapter().getItem(
				wheelMonth.getCurrentItem());
		String day = wheelDay.getAdapter().getItem(wheelDay.getCurrentItem());
		DecimalFormat df = new DecimalFormat("00");
		month = df.format(Integer.valueOf(month));
		day = df.format(Integer.valueOf(day));
		String str_date = year + "-" + month + "-" + day;
		// String str_date = year + month + day;
		return str_date;
	}

	private void setListener(View view) {
		final Button btnSure = (Button) view
				.findViewById(R.id.btn_datetime_sure);
		btnSure.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				String selectTime = getTimeString();
				btnSure.setTextColor(0xffffff);
				String[] time = selectTime.split("-");
				if (Integer.parseInt(time[0]) == YEAR_START) {
					if (Integer.parseInt(time[1]) == MOUTH_START + 1) {
						if (Integer.parseInt(time[2]) == DAY_START) {
							istrue = true;
						} else {
							istrue = false;
						}
					} else {
						istrue = false;
					}
				} else {
					istrue = false;
				}
				if (istrue) {
					chooseDateListener.chooseDate(selectTime, true);
				} else {
					chooseDateListener.chooseDate(selectTime, false);
				}
				dismiss();
			}
		});

		Button btnCancel = (Button) view.findViewById(R.id.btn_datetime_cancel);
		btnCancel.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
	}

	public void setOnChooseDateListener(ChooseDateListener chooseDateListener) {
		this.chooseDateListener = chooseDateListener;
	}

	/**
	 * selectTime 表示所选当前日期 flag 表示所选当前日期是否是今天 true表示今天
	 */
	public interface ChooseDateListener {
		void chooseDate(String selectData, boolean flag);
	}

}